package ro.fortech.peaa.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

import org.springframework.web.servlet.view.xslt.AbstractXsltView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import ro.fortech.peaa.domain.Employee;
import ro.fortech.peaa.domain.Job;

/**
 * View class for the employee details page.
 * 
 * @author andrei
 *
 */
@SuppressWarnings("deprecation")
public class EmployeeDetailsView extends AbstractXsltView {
	@SuppressWarnings("unchecked")
	public Source createXsltSource(Map model, String rootName, HttpServletRequest
	        request, HttpServletResponse response) {
    
		Document document = null;
		try {
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		}
		
	    Element root = document.createElement(rootName);
	    logger.info(rootName);
	    
	    String errorMessage = (String) model.get("error_message");
		if (errorMessage == null) {
			errorMessage = "";
		}
		Element errorMessageNode = document.createElement("error_message");
		Text errorMessageText = document.createTextNode(errorMessage);
		errorMessageNode.appendChild(errorMessageText);
		root.appendChild(errorMessageNode);
	    
	    Employee emp = (Employee) model.get("employee");
        Element employeeNode = document.createElement("employee");
        
        // employee id
        Element idNode = document.createElement("id");
        Text textNode = document.createTextNode(emp.getId().toString());
        idNode.appendChild(textNode);
        employeeNode.appendChild(idNode);
        
        // first name
        Element firstNameNode = document.createElement("first_name");
        textNode = document.createTextNode(emp.getFirstName());
        firstNameNode.appendChild(textNode);
        employeeNode.appendChild(firstNameNode);
        
        // last name
        Element lastNameNode = document.createElement("last_name");
        textNode = document.createTextNode(emp.getLastName());
        lastNameNode.appendChild(textNode);
        employeeNode.appendChild(lastNameNode);      
        
        // email
        Element emailNode = document.createElement("email");
        textNode = document.createTextNode(emp.getEmail());
        emailNode.appendChild(textNode);
        employeeNode.appendChild(emailNode);
        
        // phone_number
        Element phoneNode = document.createElement("phone");
        textNode = document.createTextNode(emp.getPhoneNumber());
        phoneNode.appendChild(textNode);
        employeeNode.appendChild(phoneNode);
        
        // hire_date
        Element hireDateNode = document.createElement("hire_date");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        textNode = document.createTextNode(dateFormat.format(emp.getHireDate()));
        hireDateNode.appendChild(textNode);
        employeeNode.appendChild(hireDateNode);
        
        // job_id
        Element jobIdNode = document.createElement("job_id");
        textNode = document.createTextNode(emp.getJobId());
        jobIdNode.appendChild(textNode);
        employeeNode.appendChild(jobIdNode);

        // salary
        Element salaryNode = document.createElement("salary");
        textNode = document.createTextNode(emp.getSalary().toString());
        salaryNode.appendChild(textNode);
        employeeNode.appendChild(salaryNode);
        
        // commision_pct
        Element commisionPctNode = document.createElement("commission_pct");
        textNode = document.createTextNode(emp.getCommissionPct().toString());
        commisionPctNode.appendChild(textNode);
        employeeNode.appendChild(commisionPctNode);
        
        // manager_id
        Element managerIdNode = document.createElement("manager_id");
        textNode = document.createTextNode(emp.getManagerId().toString());
        managerIdNode.appendChild(textNode);
        employeeNode.appendChild(managerIdNode);
        
        // department_id
        Element departmentIdNode = document.createElement("department_id");
        textNode = document.createTextNode(emp.getDepartmentId().toString());
        departmentIdNode.appendChild(textNode);
        employeeNode.appendChild(departmentIdNode);
        
        root.appendChild(employeeNode);
        
        Element jobsNode = document.createElement("jobs");
        List<Job> jobs = (List<Job>) model.get("jobs");
	    for (Iterator<Job> it = jobs.iterator(); it.hasNext();) {
	        Job nextJob = (Job) it.next();
	        Element jobNode = document.createElement("job");
	        
	        // job id
	        idNode = document.createElement("id");
	        textNode = document.createTextNode(nextJob.getId());
	        idNode.appendChild(textNode);
	        jobNode.appendChild(idNode);
	        
	        // job title
	        Element titleNode = document.createElement("title");
	        textNode = document.createTextNode(nextJob.getTitle());
	        titleNode.appendChild(textNode);
	        jobNode.appendChild(titleNode);
	        
	        jobsNode.appendChild(jobNode);
	    }
        root.appendChild(jobsNode);
	    
        Source result = new DOMSource(root);
	    return result;
	}
}
